package com.easyjava.builder;


import com.easyjava.bean.Constants;
import com.easyjava.bean.FieldInfo;
import com.easyjava.bean.TableInfo;
import com.easyjava.utils.DateUtils;
import com.easyjava.utils.StringUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedWriter;
import java.io.File;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.nio.file.Files;

/**
 * @ClassName BuildPo
 * @Date 2024/5/31  16:25
 **/
public class BuildPo {
    private static final Logger logger = LoggerFactory.getLogger(BuildPo.class);

    public static void execute(TableInfo tableInfo) {


        File folder = new File(Constants.PATH_PO);
        if (!folder.exists()) {
            folder.mkdirs();
        }
        String className = tableInfo.getBeanName();
        File file = new File(folder, className + ".java");


        try {
            OutputStream outputStream = Files.newOutputStream(file.toPath());
            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream, "utf8");
            BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);
            bufferedWriter.write("package " + Constants.PACKAGE_PO + ";");
            bufferedWriter.newLine();
            bufferedWriter.newLine();
            bufferedWriter.write("import java.io.Serializable;");
            bufferedWriter.newLine();
            bufferedWriter.write(Constants.IGNORE_JSON_CLASS);
            bufferedWriter.newLine();
            if (tableInfo.getHaveDate() || tableInfo.getHaveDateTime()) {
                bufferedWriter.write("import java.util.Date;");
                bufferedWriter.newLine();
                bufferedWriter.write("import " + Constants.PACKAGE_ENUMS + ".DateTimePatternEnum;");
                bufferedWriter.newLine();
                bufferedWriter.write("import " + Constants.PACKAGE_UTILS + ".DateUtil;");
                bufferedWriter.newLine();
                bufferedWriter.write(Constants.DATE_SERIALIZE_CLASS);
                bufferedWriter.newLine();
                bufferedWriter.write(Constants.DATE_DESERIALIZE_CLASS);
                bufferedWriter.newLine();
            }
            if (tableInfo.getHavaBigDecimal()) {
                bufferedWriter.write("import java.math.BigDecimal;");
            }
            bufferedWriter.newLine();
            BuildComment.createClassComment(bufferedWriter, tableInfo.getComment());
            bufferedWriter.write("public class " + className + " implements Serializable{");
            bufferedWriter.newLine();

            for (FieldInfo fieldInfo : tableInfo.getFieldList()) {

                BuildComment.createFieldComment(bufferedWriter, fieldInfo.getComment());
                if (ArrayUtils.contains(Constants.SQL_DATE_TIME_TYPE, fieldInfo.getSqlType())) {
                    bufferedWriter.write("\t" + String.format(Constants.DATE_SERIALIZE_EXPRESSION, DateUtils.YMD));
                    bufferedWriter.newLine();
                    bufferedWriter.write("\t" + String.format(Constants.DATE_DESERIALIZE_EXPRESSION, DateUtils.YMD1));
                    bufferedWriter.newLine();
                }

                if (ArrayUtils.contains(Constants.SQL_DATE_TYPE, fieldInfo.getSqlType())) {
                    bufferedWriter.write("\t" + String.format(Constants.DATE_SERIALIZE_EXPRESSION, DateUtils.YMD1));
                    bufferedWriter.newLine();
                    bufferedWriter.write("\t" + String.format(Constants.DATE_DESERIALIZE_EXPRESSION, DateUtils.YMD1));
                    bufferedWriter.newLine();
                }


                //忽略属性
                if (ArrayUtils.contains(Constants.IGNORE_JSON_FIELD.split(","), fieldInfo.getPropertyName())) {
                    bufferedWriter.write("\t" + String.format(Constants.IGNORE_JSON_EXPRESSION));
                    bufferedWriter.newLine();
                }

                bufferedWriter.write("\tprivate " + fieldInfo.getJavaType() + " " + fieldInfo.getPropertyName() + ";");
                bufferedWriter.newLine();
            }

            for (FieldInfo fieldInfo : tableInfo.getFieldList()) {
                bufferedWriter.newLine();
                String tempField = StringUtils.uperCaseFirstLetter(fieldInfo.getPropertyName());
                bufferedWriter.write("\tpublic "+fieldInfo.getJavaType()+" get"+tempField +"() {");
                bufferedWriter.newLine();
                bufferedWriter.write("\t\treturn " + fieldInfo.getPropertyName()+";");
                bufferedWriter.newLine();
                bufferedWriter.write("\t}");
                bufferedWriter.newLine();


                bufferedWriter.write("\tpublic void set" + tempField + "(" + fieldInfo.getJavaType()+" "+fieldInfo.getPropertyName() + ") {");
                bufferedWriter.newLine();
                bufferedWriter.write("\t\tthis." + fieldInfo.getPropertyName() + " = " + fieldInfo.getPropertyName() + ";");
                bufferedWriter.newLine();
                bufferedWriter.write("\t}");
                bufferedWriter.newLine();
            }
            //重写toString方法
            StringBuffer toString= new StringBuffer();
            for (FieldInfo fieldInfo: tableInfo.getFieldList()) {
                String properName = fieldInfo.getPropertyName();
                if (ArrayUtils.contains(Constants.SQL_DATE_TIME_TYPE, fieldInfo.getSqlType())) {
                    properName = "DateUtil.format(" + properName+",DateTimePatternEnum.YYYY_MM_DD_HH_MM_SS.getPattern())";
                }else if(ArrayUtils.contains(Constants.SQL_DATE_TYPE, fieldInfo.getSqlType())){
                    properName = "DateUtil.format(" + properName+",DateTimePatternEnum.YYYY_MM_DD.getPattern())";
                }

                toString.append(fieldInfo.getComment()+":\"+("+fieldInfo.getPropertyName()+" == null ? \"空\" : "+properName+")").append("+");
                toString.append("\",");
            }
            String toStringStr = toString.toString();
            toStringStr = "\""+toStringStr;
            toStringStr  = toStringStr.substring(0, toStringStr.lastIndexOf(",")-2);
            bufferedWriter.write("\t@Override");
            bufferedWriter.newLine();
            bufferedWriter.write("\tpublic String toString (){");
            bufferedWriter.newLine();
            bufferedWriter.write("\t\treturn " +toStringStr +";");
            bufferedWriter.newLine();
            bufferedWriter.write("\t}");
            bufferedWriter.newLine();
            bufferedWriter.write("}");
            bufferedWriter.flush();
        } catch (Exception e) {
            logger.error("创建po失败", e);
        }
    }
}

